$(function() {
    var url =window.location.search;
    if (url.indexOf("?") != -1) {
        var str = url.substr(1)
        strs = str.split("&");
        var id = strs[0].split("=")[1];
    }
    // $("option[value="+1+"]").attr("selected","selected");
    $("#id").val(id);
    $.ajax({
        type:"POST",
        url:constUrl+"CategoryServlet",
        dataType:"json",
        data: {
            operateType:"allList",
        },
        success:function(responseText){
            var typeHtml ='';
            var functionHtml = '';
            for(var i=0;i<responseText.datas.length;i++){
                if(responseText.datas[i].isFunction==1){
                    typeHtml+='<option value="'+(responseText.datas[i].categoryId)+'" >'+responseText.datas[i].categoryName+'</option>'
                }else if(responseText.datas[i].isFunction==0){
                    functionHtml+='<option value="'+(responseText.datas[i].categoryId)+'" >'+responseText.datas[i].categoryName+'</option>'
                }

            }
            $("#algorithm_type").append(typeHtml);
            $("#algorithm_function").append(functionHtml);
        }
    });
    $.ajax({
        url:constUrl+"CompanyServlet",
        type:'post',
        data:{
            operateType:"showAll",
            onePageCount:"10",
            currentPage:"1"
        },
        dataType:'json',
        success: function(responseText) {
            var companyHtml ='';
            for(var i=0;i<responseText.datas.length;i++){
                companyHtml+='<option value="'+(responseText.datas[i].companyId)+'" >'+responseText.datas[i].companyName+'</option>'

            }
            $("#company_name").append(companyHtml);
        },
        complete: function() {
        },
        error: function() {
        }
    });
    $.ajax({
        type:"POST",
        url:constUrl+"AlgorithmServlet",
        dataType:"json",
        data: {
            operateType:"showDetail",
            algorithmId:id
        },
        success:function(data){
            console.log(data);
            addDetail(data.result[0]);
        }
    });

});
//关闭layer页面功能`
$("#closeLayer").click(function () {
    var index =parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
});


//向页面添加数据
function addDetail(data) {

    $("#name").val(data.name);
    $("#version").val(data.version);
    // $("#function").val(data[id].function);
    console.log(data.type.categoryId);
    console.log(data.function.categoryId);
    $("option[value="+data.function.categoryId+"]").attr("selected","selected");
    $("option[value="+data.type.categoryId+"]").attr("selected","selected");
    // $("option[value="+data[i].function+"]").attr(selected)
    $("option[value="+data.companyName+"]").attr("selected","selected");
    // $("#company_name").val(data.companyName);
    $("#introduction").val(data.introduction);
    var postfix = data.postfix.split(".")[1];
    $("option[value="+postfix+"]").attr("selected","selected");
    $("option[value="+data.platform+"]").attr("selected","selected");

    var containGpu = data.containGpu;
    var useMpi = data.useMpi;
    var genetic = data.genetic;
    console.log(containGpu);
    if (containGpu){
        $("#is_use_gpu").attr("checked","checked" );
    }else{
        $("#no_gpu").attr("checked","checked");
    }
    if  (useMpi){
        $("#is_use_mpi").attr("checked","checked");
        $("#cpunum_div").show();
        $("#cpunum").val(data.cpuNum);
    }else{
        $("#no_mpi").attr("checked","checked");
    }
    if  (genetic){
        $("#isGenetic").attr("checked","checked");
    }else{
        $("#noGenetic").attr("checked","checked");
    }


//输入文件
    var algorithmInputs = data.algorithmInputs;
    $("#Num").text(algorithmInputs.length);
    for (var i = 0; i<algorithmInputs.length; i++){
        addcounti(i);
    }

    $("#addInputBtn").click(function () {
        addcount();
    });
    $("#deleteInputBtn").click(function () {
        deletecount();
    });
    function addcounti(inputLength) {

        var Area = document.getElementById("InputArea");
        var Num = document.getElementById("Num");

        var temp = inputLength
        temp++;
        var inputpara = "inputpara"+temp;
        var para = "para" + temp;
        var inputName= "input_name" + temp;
        var inputType = "input_type"+temp;
        var inputDetail = "input_introduction"+ temp;
        var inputSuffix = "input_suffix"+temp;
        var inputTypeDiv = "input_type_div"+temp;
        var inputIntroDiv = "input_introduction_div"+temp;
        var inputSuffixDiv = "input_suffix_div"+temp;
        var acplaceholder = "名称"+temp;
        var cinDetailPlaceholder = "描述";
        var suffixAcplaceholder = "后缀"+temp;
        var inputHtml = "";
        if(algorithmInputs[inputLength].type=="file"){
           inputHtml = '<div class="row '+inputpara+'" id="'+inputpara+'">' +
                '<div class="col-md-3 areaInput '+para+'">' +
                '        <input type="text" name="'+inputName+'" id="'+inputName+'" placeholder="'+acplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
                ' </div>' +
                ' <div class="col-md-3 areaInput '+para+'">' +
                '        <select id="'+inputType+'" class="col-md-3"style="height: 37px;font-size:small"  >' +
                '                <option value="file" selected="selected">文件</option>' +
                '               <option value="directory">目录</option>' +
                '          </select>' +
                ' </div>'+
                '<div class="col-md-3 areaInput '+para+'" id="'+inputIntroDiv+'">' +
                '        <input type="text" placeholder="'+cinDetailPlaceholder+'" name="'+inputDetail+'" id="'+inputDetail+'" style="display: inline-block;font-size:small; height: 37px"">' +
                ' </div> ' +
                ' <div class="col-md-3 areaInput '+para+'" name="'+inputSuffixDiv+'">' +
                '        <input type="text" name="'+inputSuffix+'" id="'+inputSuffix+'" placeholder="'+suffixAcplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
                '</div>'+
                '</div>';
            $("#InputArea").append(inputHtml);
            console.log(algorithmInputs[inputLength].type);
            $("#"+inputName).val(algorithmInputs[inputLength].inputName);
            $("#"+inputType).val(algorithmInputs[inputLength].type);
            $("#"+inputDetail).val(algorithmInputs[inputLength].inputIntroduction);
            $("#"+inputSuffix).val(algorithmInputs[inputLength].suffix);
            var sel = document.getElementById(inputType);
            sel.onchange = function () {
                if(sel.options[sel.selectedIndex].value=="directory"){
                    $('#'+inputpara+'>div[name="'+inputSuffixDiv+'"]').remove();
                    $('#'+inputIntroDiv).attr("class","col-md-6 areaInput "+para);

                }else if(sel.options[sel.selectedIndex].value=="file"){
                    $('#'+inputpara+'>div[name="'+inputSuffixDiv+'"]').remove();
                    $('#'+inputIntroDiv).attr("class","col-md-3 areaInput "+para);
                    var html2 = ' <div class="col-md-3 areaInput '+para+'" name="'+inputSuffixDiv+'">' +
                        '        <input type="text" name="'+inputSuffix+'" id="'+inputSuffix+'" placeholder="'+suffixAcplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
                        '</div>';
                    $('#'+inputpara).append(html2);
                }
            }

        }else if(algorithmInputs[inputLength].type=="directory"){
            inputHtml = '<div class="row '+inputpara+'" id="'+inputpara+'">' +
                '<div class="col-md-3 areaInput '+para+'">' +
                '        <input type="text" name="'+inputName+'" id="'+inputName+'" placeholder="'+acplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
                ' </div>' +
                ' <div class="col-md-3 areaInput '+para+'">' +
                '        <select id="'+inputType+'" class="col-md-3"style="height: 37px;font-size:small"  >' +
                '                <option value="file" selected="selected">文件</option>' +
                '               <option value="directory">目录</option>' +
                '          </select>' +
                ' </div>'+
                '<div class="col-md-6 areaInput '+para+'" id="'+inputIntroDiv+'">' +
                '        <input type="text" placeholder="'+cinDetailPlaceholder+'" name="'+inputDetail+'" id="'+inputDetail+'" style="display: inline-block;font-size:small; height: 37px"">' +
                ' </div> ' +
                '</div>';
            $("#InputArea").append(inputHtml);
            console.log(algorithmInputs[inputLength].type);
            $("#"+inputName).val(algorithmInputs[inputLength].inputName);
            $("#"+inputType).val(algorithmInputs[inputLength].type);
            $("#"+inputDetail).val(algorithmInputs[inputLength].inputIntroduction);
            var sel = document.getElementById(inputType);
            sel.onchange = function () {
                if(sel.options[sel.selectedIndex].value=="directory"){
                    $('#'+inputpara+'>div[name="'+inputSuffixDiv+'"]').remove();
                    $('#'+inputIntroDiv).attr("class","col-md-6 areaInput "+para);

                }else if(sel.options[sel.selectedIndex].value=="file"){
                    $('#'+inputpara+'>div[name="'+inputSuffixDiv+'"]').remove();
                    $('#'+inputIntroDiv).attr("class","col-md-3 areaInput "+para);
                    var html2 = ' <div class="col-md-3 areaInput '+para+'" name="'+inputSuffixDiv+'">' +
                        '        <input type="text" name="'+inputSuffix+'" id="'+inputSuffix+'" placeholder="'+suffixAcplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
                        '</div>';
                    $('#'+inputpara).append(html2);
                }
            }
        }


        //删除按钮
        var deleteBtn = document.getElementById("deleteInputBtn");
        deleteBtn.removeAttribute("disabled");
        return false;
    }

    function addcount() {
        var Num = document.getElementById("Num");
        var temp = Num.innerHTML;
        temp++;
        console.log(temp);
        Num.innerHTML = temp;
        var inputpara = "inputpara"+temp;
        var para = "para" + temp;
        var inputName= "input_name" + temp;
        var inputType = "input_type"+temp;
        var inputDetail = "input_introduction"+ temp;
        var inputSuffix = "input_suffix"+temp;
        var inputTypeDiv = "input_type_div"+temp;
        var inputIntroDiv = "input_introduction_div"+temp;
        var inputSuffixDiv = "input_suffix_div"+temp;
        var acplaceholder = "名称"+temp;
        var cinDetailPlaceholder = "描述";
        var suffixAcplaceholder = "后缀"+temp;


        var inputHtml = '<div class="row '+inputpara+'" id="'+inputpara+'">' +
            '<div class="col-md-3 areaInput '+para+'">' +
            '        <input type="text" name="'+inputName+'" id="'+inputName+'" placeholder="'+acplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
            ' </div>' +
            ' <div class="col-md-3 areaInput '+para+'">' +
            '        <select id="'+inputType+'" class="col-md-3"style="height: 37px;font-size:small"  >' +
            '                <option value="file" selected="selected">文件</option>' +
            '               <option value="directory">目录</option>' +
            '          </select>' +
            ' </div>'+
            '<div class="col-md-3 areaInput '+para+'" id="'+inputIntroDiv+'">' +
            '        <input type="text" placeholder="'+cinDetailPlaceholder+'" name="'+inputDetail+'" id="'+inputDetail+'" style="display: inline-block;font-size:small; height: 37px"">' +
            ' </div> ' +
            ' <div class="col-md-3 areaInput '+para+'" name="'+inputSuffixDiv+'">' +
            '        <input type="text" name="'+inputSuffix+'" id="'+inputSuffix+'" placeholder="'+suffixAcplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
            '</div>'+
            '</div>'
        $("#InputArea").append(inputHtml);

        var sel = document.getElementById(inputType);
        sel.onchange = function () {
            if(sel.options[sel.selectedIndex].value=="directory"){
                $('#'+inputpara+'>div[name="'+inputSuffixDiv+'"]').remove();
                $('#'+inputIntroDiv).attr("class","col-md-6 areaInput "+para);

            }else if(sel.options[sel.selectedIndex].value=="file"){
                $('#'+inputpara+'>div[name="'+inputSuffixDiv+'"]').remove();
                $('#'+inputIntroDiv).attr("class","col-md-3 areaInput "+para);
                var html2 = ' <div class="col-md-3 areaInput '+para+'" name="'+inputSuffixDiv+'">' +
                    '        <input type="text" name="'+inputSuffix+'" id="'+inputSuffix+'" placeholder="'+suffixAcplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
                    '</div>';
                $('#'+inputpara).append(html2);
            }
        }
        //删除按钮
        var deleteBtn = document.getElementById("deleteInputBtn");
        deleteBtn.removeAttribute("disabled");
        return false;
    }

//删除输入数量
    function deletecount() {
        // event.preventDefault();
        var Area = document.getElementById("InputArea");
        // var addNum = document.getElementById("addNum");
        var Num = document.getElementById("Num");

        var temp = Num.textContent;

        var deleteLabel = document.getElementsByClassName("inputpara"+temp);
        Area.removeChild(deleteLabel[0]);
        temp--;
        Num.textContent = temp;
        console.log(temp);
        if(temp === 0){
            var deleteBtn = document.getElementById("deleteInputBtn");
            deleteBtn.setAttribute("disabled","true");
        }
        return false;
    }


//输出数量
    var algorithmOutputs = data.algorithmOutputs;
    $("#outputNum").text(algorithmOutputs.length);
    for (var i = 0; i<algorithmOutputs.length;i++){
        addCouti(i);
    }
    $("#addOutputBtn").click(function () {
        addCout();
    });
    $("#deleteOutputBtn").click(function () {
        deletecout();
    });
//增加输出数量
    function addCouti(outputLength) {

        // event.preventDefault();
        var Area = document.getElementById("outputArea");
        var Num = document.getElementById("outputNum");
        var temp = outputLength
        temp++;
        var outpara = "outpara"+temp;
        var para = "coutPara" + temp;
        var outputName= "output_name" + temp;
        var outputType = "output_type"+temp;
        var outputDetail = "output_introduction" + temp;
        var outputSuffix = "output_suffix"+temp;
        var outputTypeDiv = "output_type_div"+temp;
        var outputIntroDiv = "output_introduction_div"+temp;
        var outputSuffixDiv = "output_suffix_div"+temp;
        var acplaceholder = "名称"+temp;
        var cinDetailPlaceholder = "描述";
        var suffixAcplaceholder = "后缀"+temp;

        if(algorithmOutputs[outputLength].type =="file" ){
            var outputHtml = '<div class="row '+outpara+'" id="'+outpara+'">' +
                '<div class="col-md-3 areaInput '+para+'">' +
                '        <input type="text" name="'+outputName+'" id="'+outputName+'" placeholder="'+acplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
                '</div>' +
                ' <div class="col-md-3 areaInput '+para+'">' +
                '        <select id="'+outputType+'" class="col-md-3"  style="height: 37px;font-size:small">' +
                '                <option value="file" selected="selected">文件</option>' +
                '                <option value="directory">目录</option>' +
                '            </select>' +
                '    </div>'+
                ' <div class="col-md-3  areaInput '+para+'" id="'+outputIntroDiv+'">' +
                '        <input type="text" placeholder="'+cinDetailPlaceholder+'" name="'+outputDetail+'" id="'+outputDetail+'" style="display: inline-block;font-size:small; height: 37px">' +
                ' </div>' +
                ' <div class="col-md-3  areaInput '+para+'" name="'+outputSuffixDiv+'">' +
                '        <input type="text" placeholder="'+suffixAcplaceholder+'" name="'+outputSuffix+'" id="'+outputSuffix+'" style="display: inline-block;font-size:small; height: 37px">' +
                ' </div>' +
                '</div>'
            $("#outputArea").append(outputHtml);

            $("#"+outputName).val(algorithmOutputs[outputLength].outputName);
            // $("option[value="+algorithmOutputs[outputLength].type+"]").attr("selected","selected");
            $("#"+outputType).val(algorithmOutputs[outputLength].type);
            $("#"+outputDetail).val(algorithmOutputs[outputLength].outputIntroduction);
            $("#"+outputSuffix).val(algorithmOutputs[outputLength].suffix);
            var sel = document.getElementById(outputType);
            sel.onchange = function () {
                if(sel.options[sel.selectedIndex].value=="directory"){
                    $('#'+outpara+'>div[name="'+outputSuffixDiv+'"]').remove();
                    $('#'+outputIntroDiv).attr("class","col-md-6 areaInput "+para);

                }else if(sel.options[sel.selectedIndex].value=="file"){
                    $('#'+outpara+'>div[name="'+outputSuffixDiv+'"]').remove();
                    $('#'+outputIntroDiv).attr("class","col-md-3 areaInput "+para);
                    var html2 = ' <div class="col-md-3 areaInput '+para+'" name="'+outputSuffixDiv+'">' +
                        '        <input type="text" name="'+outputSuffix+'" id="'+outputSuffix+'" placeholder="'+suffixAcplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
                        '</div>';
                    $('#'+outpara).append(html2);
                }
            }

        }else if(algorithmOutputs[outputLength].type =="directory"){
            var outputHtml = '<div class="row '+outpara+'" id="'+outpara+'">' +
                '<div class="col-md-3 areaInput '+para+'">' +
                '        <input type="text" name="'+outputName+'" id="'+outputName+'" placeholder="'+acplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
                '</div>' +
                ' <div class="col-md-3 areaInput '+para+'">' +
                '        <select id="'+outputType+'" class="col-md-3"  style="height: 37px;font-size:small">' +
                '                <option value="file" selected="selected">文件</option>' +
                '                <option value="directory">目录</option>' +
                '            </select>' +
                '    </div>'+
                ' <div class="col-md-6 areaInput '+para+'" id="'+outputIntroDiv+'">' +
                '        <input type="text" placeholder="'+cinDetailPlaceholder+'" name="'+outputDetail+'" id="'+outputDetail+'" style="display: inline-block;font-size:small; height: 37px">' +
                ' </div>' +
                '</div>'
            $("#outputArea").append(outputHtml);

            $("#"+outputName).val(algorithmOutputs[outputLength].outputName);
            // $("option[value="+algorithmOutputs[outputLength].type+"]").attr("selected","selected");
            $("#"+outputType).val(algorithmOutputs[outputLength].type);
            $("#"+outputDetail).val(algorithmOutputs[outputLength].outputIntroduction);
            var sel = document.getElementById(outputType);
            sel.onchange = function () {
                if(sel.options[sel.selectedIndex].value=="directory"){
                    $('#'+outpara+'>div[name="'+outputSuffixDiv+'"]').remove();
                    $('#'+outputIntroDiv).attr("class","col-md-6 areaInput "+para);

                }else if(sel.options[sel.selectedIndex].value=="file"){
                    $('#'+outpara+'>div[name="'+outputSuffixDiv+'"]').remove();
                    $('#'+outputIntroDiv).attr("class","col-md-3 areaInput "+para);
                    var html2 = ' <div class="col-md-3 areaInput '+para+'" name="'+outputSuffixDiv+'">' +
                        '        <input type="text" name="'+outputSuffix+'" id="'+outputSuffix+'" placeholder="'+suffixAcplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
                        '</div>';
                    $('#'+outpara).append(html2);
                }
            }
        }


        //删除按钮
        var deleteBtn = document.getElementById("deleteOutputBtn");
        deleteBtn.removeAttribute("disabled");
        return false;
    }

    function addCout() {
        // event.preventDefault();
        var Area = document.getElementById("outputArea");
        var Num = document.getElementById("outputNum");
        var temp = Num.innerHTML;
        temp++;
        Num.innerHTML = temp;
        var outpara = "outpara"+temp;
        var para = "coutPara" + temp;
        var outputName= "output_name" + temp;
        var outputType = "output_type"+temp;
        var outputDetail = "output_introduction" + temp;
        var outputSuffix = "output_suffix"+temp;
        var outputTypeDiv = "output_type_div"+temp;
        var outputIntroDiv = "output_introduction_div"+temp;
        var outputSuffixDiv = "output_suffix_div"+temp;
        var acplaceholder = "名称"+temp;
        var cinDetailPlaceholder = "描述";
        var suffixAcplaceholder = "后缀"+temp;



        var outputHtml = '<div class="row '+outpara+'" id="'+outpara+'">' +
            '<div class="col-md-3 areaInput '+para+'">' +
            '        <input type="text" name="'+outputName+'" id="'+outputName+'" placeholder="'+acplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
            '</div>' +
            ' <div class="col-md-3 areaInput '+para+'">' +
            '        <select id="'+outputType+'" class="col-md-3"  style="height: 37px;font-size:small">' +
            '                <option value="file" selected="selected">文件</option>' +
            '                <option value="directory">目录</option>' +
            '            </select>' +
            '    </div>'+
            ' <div class="col-md-3  areaInput '+para+'" id="'+outputIntroDiv+'">' +
            '        <input type="text" placeholder="'+cinDetailPlaceholder+'" name="'+outputDetail+'" id="'+outputDetail+'" style="display: inline-block;font-size:small; height: 37px">' +
            ' </div>' +
            ' <div class="col-md-3  areaInput '+para+'" name="'+outputSuffixDiv+'">' +
            '        <input type="text" placeholder="'+suffixAcplaceholder+'" name="'+outputSuffix+'" id="'+outputSuffix+'" style="display: inline-block;font-size:small; height: 37px">' +
            ' </div>' +
            '</div>'
        $("#outputArea").append(outputHtml);
        var sel = document.getElementById(outputType);
        sel.onchange = function () {
            if(sel.options[sel.selectedIndex].value=="directory"){
                $('#'+outpara+'>div[name="'+outputSuffixDiv+'"]').remove();
                $('#'+outputIntroDiv).attr("class","col-md-6 areaInput "+para);

            }else if(sel.options[sel.selectedIndex].value=="file"){
                $('#'+outpara+'>div[name="'+outputSuffixDiv+'"]').remove();
                $('#'+outputIntroDiv).attr("class","col-md-3 areaInput "+para);
                var html2 = ' <div class="col-md-3 areaInput '+para+'" name="'+outputSuffixDiv+'">' +
                    '        <input type="text" name="'+outputSuffix+'" id="'+outputSuffix+'" placeholder="'+suffixAcplaceholder+'" style="display: inline-block;font-size:small; height: 37px">' +
                    '</div>';
                $('#'+outpara).append(html2);
            }
        }
        //删除按钮
        var deleteBtn = document.getElementById("deleteOutputBtn");
        deleteBtn.removeAttribute("disabled");
        return false;
    }

//删除输出数量
    function deletecout() {

        // event.preventDefault();
        var Area = document.getElementById("outputArea");
        var Num = document.getElementById("outputNum");
        var temp = Num.textContent;
        var deleteLabel = document.getElementsByClassName("outpara"+temp);
        Area.removeChild(deleteLabel[0]);
        temp--;
        Num.textContent = temp
        if(temp === 0){
            var deleteBtn = document.getElementById("deleteOutputBtn");
            deleteBtn.setAttribute("disabled","true")
        }
        return false;
    }

//输入参数
    var algorithmParams = data.algorithmParams;
    $("#paramNum").text(algorithmParams.length);
    for (var i = 0; i<algorithmParams.length; i++){
        addCinParai(i);
    }
    $("#addParamBtn").click(function () {
        addCinPara();
    });
    $("#deleteParamBtn").click(function () {
        deleteCinPara();
    });
//增加输入参数
    function addCinParai(paraLength) {

        // event.preventDefault();
        var Area = document.getElementById("paramArea");
        var temp = paraLength;
        temp++;
        var parames = "parames"+temp;
        var para = "cinPara" + temp;
        var paramType =  "param_id" + temp;
        var paramSymbol=  "param_symbol" + temp;
        var paramDetail=  "param_introduction" + temp;
        var paramDefaultValue = "paramDefaultValue"+temp;

        var symbolPlace = "符号"+ temp;
        var cinDetailPlaceholder = "描述"+temp;
        var defalutPlaceholder = "默认值"+temp;


        var paramHtml = '<div class="row '+parames+'"><div class="col-md-3 areaInput '+para+'">' +
            '        <input type="text" name="'+paramSymbol+'" id="'+paramSymbol+'" placeholder="'+symbolPlace+'" style="display: inline-block;font-size:small; height: 37px">' +
            '    </div>' +
            ' <div class="col-md-3 areaInput '+para+'">' +
            '        <select id="'+paramType+'" class="col-md-3"  style="' +
            '    height: 37px;font-size:small' +
            '"  >' +
            '                <option value="int">int</option>' +
            '                <option value="double">double</option>' +
            '                <option value="float">float</option>' +
            '                <option value="string">string</option>' +
            '            </select>' +
            '    </div>'+
            '    <div class="col-md-3 areaInput '+para+'">' +
            '        <input type="text" placeholder="'+cinDetailPlaceholder+'" name="'+paramDetail+'" id="'+paramDetail+'" style="display: inline-block;font-size:small; height: 37px">' +
            '  </div> ' +
            '    <div class="col-md-3 areaInput '+para+'">' +
            '        <input type="text" placeholder="'+defalutPlaceholder+'" name="'+paramDefaultValue+'" id="'+paramDefaultValue+'" style="display: inline-block;font-size:small; height: 37px">' +
            '  </div> '+
            '</div>'
        $("#paramArea").append(paramHtml);
        $("#"+paramSymbol).val(algorithmParams[paraLength].paramSymbol);
        $("#"+paramType).val(algorithmParams[paraLength].paramType);
        // $("option[value="+algorithmParams[paraLength].paramType+"]").attr("selected","selected");
        $("#"+paramDetail).val(algorithmParams[paraLength].paramIntroduction);
        $("#"+paramDefaultValue).val(algorithmParams[paraLength].paramDefaultValue);

        //删除按钮
        var deleteBtn = document.getElementById("deleteParamBtn");
        deleteBtn.removeAttribute("disabled");
        // console.log(deleteBtn);
        return false;
    }

    function addCinPara() {

        var Area = document.getElementById("paramArea");
        var Num = document.getElementById("paramNum");
        var temp = Num.innerHTML;
        temp++;
        Num.innerHTML = temp;
        var parames = "parames"+temp;
        var para = "cinPara" + temp;
        var paramType =  "param_id" + temp;
        var paramSymbol=  "param_symbol" + temp;
        var paramDetail=  "param_introduction" + temp;
        var paramDefaultValue = "paramDefaultValue"+temp;

        var symbolPlace = "符号"+ temp;
        var cinDetailPlaceholder = "描述"+temp;
        var defalutPlaceholder = "默认值"+temp;

        var paramHtml = '<div class="row '+parames+'"><div class="col-md-3 areaInput '+para+'">' +
            '        <input type="text" name="'+paramSymbol+'" id="'+paramSymbol+'" placeholder="'+symbolPlace+'" style="display: inline-block;font-size:small; height: 37px">' +
            '    </div>' +
            ' <div class="col-md-3 areaInput '+para+'">' +
            '        <select id="'+paramType+'" class="col-md-3"  style="' +
            '    height: 37px;font-size:small' +
            '"  >' +
            '                <option value="int">int</option>' +
            '                <option value="double">double</option>' +
            '                <option value="float">float</option>' +
            '                <option value="string">string</option>' +
            '            </select>' +
            '    </div>'+
            '    <div class="col-md-3 areaInput '+para+'">' +
            '        <input type="text" placeholder="'+cinDetailPlaceholder+'" name="'+paramDetail+'" id="'+paramDetail+'" style="display: inline-block;font-size:small; height: 37px">' +
            '    </div>' +
            '    <div class="col-md-3 areaInput '+para+'">' +
            '        <input type="text" placeholder="'+defalutPlaceholder+'" name="'+paramDefaultValue+'" id="'+paramDefaultValue+'" style="display: inline-block;font-size:small; height: 37px">' +
            '    </div>'  +
            '</div>'
        $("#paramArea").append(paramHtml);

        //删除按钮
        var deleteBtn = document.getElementById("deleteParamBtn");
        deleteBtn.removeAttribute("disabled");
        // console.log(deleteBtn);
        return false;
    }

//删除输入参数
    function deleteCinPara() {
        // event.preventDefault();
        var Area = document.getElementById("paramArea");
        var Num = document.getElementById("paramNum");
        var temp = Num.textContent;
        var deleteLabel = document.getElementsByClassName("parames"+temp);
        Area.removeChild(deleteLabel[0]);
        temp--;
        Num.textContent = temp;
        if(temp === 0){
            var deleteBtn = document.getElementById("deleteParamBtn");
            deleteBtn.setAttribute("disabled","true")
        }
    }
    function RepNumber(obj) {
        obj.value =obj.value.replace(/[^\d.]/g, "");
        if(obj.value>100){
            obj.value=100;
        }
        return false;
    }


}

function update(){
    // event.preventDefault();
    var updateinfo=new Object();
    updateinfo.id=document.getElementById("id").value;
    updateinfo.name=document.getElementById("name").value;
    input_count_num=document.getElementById("Num").textContent;
    var inputinfo=new Array();
    for(var i=0;i< input_count_num;i++)
    {
        var temp=i+1;
        var inputName= "input_name" +temp;
        var type = "input_type"+temp;
        var inputDetail = "input_introduction"+temp;
        var inputSufficcx = "input_suffix"+temp;
        if(document.getElementById(type).value=="file"){
            inputinfo.push({
                inputName:document.getElementById(inputName).value,
                type:document.getElementById(type).value,
                inputIntroduction:document.getElementById(inputDetail).value,
                suffix:document.getElementById(inputSufficcx).value
            })
        }else if(document.getElementById(type).value=="directory"){
            inputinfo.push({
                inputName:document.getElementById(inputName).value,
                type:document.getElementById(type).value,
                inputIntroduction:document.getElementById(inputDetail).value,
                suffix:""
            })
        }
    }
    // console.log(inputinfo);
    updateinfo.inputCount=inputinfo;
    output_count_num=document.getElementById("outputNum").textContent;
    // console.log( output_count_num);
    var outputinfo=new Array();
    for(var i=0;i< output_count_num;i++)
    {
        var temp=i+1;
        var outputName= "output_name" +temp;
        var type = "output_type"+temp;
        var outputDetail = "output_introduction" +temp;
        var outputSuffix = "output_suffix"+temp;
        if(document.getElementById(type).value=="file"){
            outputinfo.push({
                outputName:document.getElementById(outputName).value,
                type:document.getElementById(type).value,
                outputIntroduction:document.getElementById(outputDetail).value,
                suffix:document.getElementById(outputSuffix).value
            })
        }else if(document.getElementById(type).value=="directory"){
            outputinfo.push({
                outputName:document.getElementById(outputName).value,
                type:document.getElementById(type).value,
                outputIntroduction:document.getElementById(outputDetail).value,
                suffix:""
            })
        }
    }
    // console.log(outputinfo);
    updateinfo.outputCount=outputinfo;
    params_count_num=document.getElementById("paramNum").textContent;
    // console.log(params_count_num);
    var paramInfo=new Array();
    for(var i=0;i<params_count_num;i++)
    {
        var temp=i+1;
        var paramName =  "param_id" +temp;
        var paramSymbol=  "param_symbol" +temp;
        var paramDetail=  "param_introduction" + temp;
        var paramDefaultValue = "paramDefaultValue"+temp;
        paramInfo.push({
            paramSymbol:document.getElementById(paramSymbol).value,
            paramType:document.getElementById(paramName).value,
            paramIntroduction:document.getElementById(paramDetail).value,
            paramDefaultValue:document.getElementById(paramDefaultValue).value
        })
    }
    // console.log(paramInfo);
    updateinfo.paramsCount=paramInfo;
    updateinfo.version=document.getElementById("version").value;
    updateinfo.introduction=document.getElementById("introduction").value;
    updateinfo.algorithmType=document.getElementById("algorithm_type").value;
    updateinfo.postfix="."+document.getElementById("postfix").value;
    updateinfo.companyName=document.getElementById("company_name").value;
    updateinfo.platform=document.getElementById("platform").value;
    var cpunum = document.getElementById("cpunum").value;
    if(cpunum==""){
        updateinfo.cpuNum = 0;
    }else{
        updateinfo.cpuNum = cpunum;
    }
    updateinfo.algorithmFunction=document.getElementById("algorithm_function").value;
    var GPU=document.getElementsByName("GPU");
    for(var i=0;i<GPU.length;i++)
    {
        if(GPU[i].checked)
        {
            updateinfo.isContainGpu=GPU[i].value;
            break;
        }
        else
        {
            continue;
        }
    }
    var MPI=document.getElementsByName("MPI")
    for(var i=0;i<MPI.length;i++)
    {
        if(MPI[i].checked)
        {
            updateinfo.isUseMpi=MPI[i].value;
            break;
        }
        else
        {
            continue;
        }
    }
    var genetic=document.getElementsByName("genetic")
    for(var i=0;i<genetic.length;i++)
    {
        if(genetic[i].checked)
        {
            updateinfo.genetic=genetic[i].value;
            break;
        }
        else
        {
            continue;
        }
    }
    var updateinfo_json = JSON.stringify(updateinfo);
    console.log(updateinfo_json);
    var file=new FormData();
    file.append('algorithm',updateinfo_json);
    // console.log(updateinfo_json);
    // console.log(file.get("algorithm"));
    console.log(file);
    var validators = new  InputValidators();

    validators.importStrategies(validationStrategies);

    validators.addValidationStrategy('isEqual',  function(element, errMsg, value1, value2) {
        if(value1 !== value2) {
            return this.buildInvalidObj(element, errMsg, value1 );
        }
    });
    //算法名称
    var ag_name = document.getElementById('name');
    validators.addValidator('isNoEmpty', ag_name, '算法名称未填写，请返回填写', ag_name.value);
    // validators.addValidator('minLength:3', ag_name, '算法名称的字符个数必须是3到20个', ag_name.value);
    // validators.addValidator('maxLength:20', ag_name, '算法名称的字符个数必须是3到20个', ag_name.value);
    //算法版本号
    var ag_version = document.getElementById('version');
    validators.addValidator('isNoEmpty', ag_version, '算法版本未填写，请返回填写', ag_version.value);
    //算法功能选择
    var ag_function = document.getElementById('algorithm_function');
    validators.addValidator('isNoEmpty', ag_function, '算法功能未选择，请返回选择', ag_function.value);
    //算法类型选择
    var ag_type = document.getElementById('algorithm_type');
    validators.addValidator('isNoEmpty', ag_type, '算法类型未选择，请返回选择', ag_type.value);
    //文件
   /* var paramFlieresString=$("#file");
    var str = paramFlieresString.val().split("\\");
    var paramFlieres = str[str.length-1];
    validators.addValidator('isNoEmpty', paramFlieresString, '没有选择文件，请返回选择文件', paramFlieres);*/
    //算法研制单位
    var ag_company = document.getElementById('company_name');
    validators.addValidator('isNoEmpty', ag_company, '算法研制单位未选择，请返回选择', ag_company.value);
    //算法简介
    var ag_introduction =document.getElementById("introduction");
    validators.addValidator('isNoEmpty', ag_introduction, '算法简介未填写，请返回填写', ag_introduction.value);
    // validators.addValidator('minLength:3', ag_introduction, '算法简介包含的字符少于3个字符，请返回选择', ag_introduction.value);
    // validators.addValidator('maxLength:250', ag_introduction, '算法类型未选择，请返回选择', ag_introduction.value);
    //算法后缀
    var ag_postfix = document.getElementById('postfix');
    validators.addValidator('isNoEmpty', ag_postfix, '算法后缀未选择，请返回选择', ag_postfix.value);
    //算法使用平台
    var ag_platform = document.getElementById('platform');
    validators.addValidator('isNoEmpty', ag_platform, '算法使用平台未选择，请返回选择', ag_platform.value);
    //是否使用gpu
    var ag_gpu=document.getElementsByName("GPU");
    validators.addValidator('isCheck', ag_gpu, '请选择是否使用GPU', ag_gpu);
    //是否使用MPI
    var ag_mpi=document.getElementsByName("MPI");
    validators.addValidator('isCheck', ag_mpi, '请选择是否使用MPI', ag_mpi);
    //检查cpu个数是否正确
    if($("input[name=MPI]:checked").attr("id")=="is_use_mpi"){
        var ag_cpunum = document.getElementById("cpunum")
        validators.addValidator('isInteger', ag_cpunum, '请正确填写CPU个数', ag_cpunum.value);
    }
    //是否是遗留算法
    var ag_genetic=document.getElementsByName("genetic");
    validators.addValidator('isCheck', ag_genetic, '请选择是否是遗留算法', ag_genetic);

    // for(var i = 0;i<input_count_num;i++){
    //     var temp = i+1;
    //     var type = "input_type"+temp;
    //     if(document.getElementById(type).value=="file"){
    //         var inputSuffix = "input_suffix"+temp;
    //         var ag_inputSuffix = document.getElementById(inputSuffix);
    //         validators.addValidator('isNotEmpty', ag_inputSuffix, '请正确填写第'+temp+'个输入参数的后缀,后缀以“.”开头且长度不小于1', ag_inputSuffix.value);
    //     }
    // }
    //
    // for(var i = 0;i<output_count_num;i++){
    //     var temp = i+1;
    //     var type = "output_type"+temp;
    //     if(document.getElementById(type).value=="file"){
    //         var outputSuffix = "output_suffix"+temp;
    //         var ag_outputSuffix = document.getElementById(outputSuffix);
    //         validators.addValidator('isNotEmpty', ag_outputSuffix, '请正确填写第'+temp+'个输出参数的后缀,后缀以“.”开头且长度不小于1', ag_outputSuffix.value);
    //     }
    // }

    var result = validators.check();
    if(result){
        layer.msg(result.errMsg);
        // alert(result.errMsg);
        result.element.focus();
        result.element.select();
        return false;
    }
    console.log(document.getElementById('file').value);
    if(document.getElementById('file').value==''){
        var fileinfo=document.getElementById("file");
        file.append("coverFlag",true);
        file.append('file',fileinfo.files[0]);
        $.ajax({
            type:"POST",
            url:constUrl+"RegisterAlgorithm",
            processData: false,
            contentType: false,
            // dataType:"json",
            data: file,
            success:function(responseText){
                var res = JSON.stringify(responseText);
                responseText=eval("("+responseText+")");
                if(responseText.result==true)
                {
                    layer.confirm('修改算法成功！',{
                        btn:['确定']
                    },function () {
                        // layer.msg('添加成功',{icon:1});
                        var index = parent.layer.getFrameIndex(window.name);
                        // parent.layer.close(index);
                        parent.location.reload(index);
                    })

                }
                else
                {
                    layer.msg("修改算法失败："+responseText.message);
                }
            },
            error:function(responseText) {
                layer.msg("修改算法失败："+responseText.message);
            },
        })
    }else{
        layer.confirm('请选择是否新增算法版本', {
            btn: ['是','否'] //按钮
        }, function(){
            var fileinfo=document.getElementById("file");
            file.append("coverFlag",false);
            file.append('file',fileinfo.files[0]);
            $.ajax({
                type:"POST",
                url:constUrl+"RegisterAlgorithm",
                processData: false,
                contentType: false,
                // dataType:"json",
                data: file,
                success:function(responseText){
                    var res = JSON.stringify(responseText);
                    responseText=eval("("+responseText+")");
                    if(responseText.result==true)
                    {
                        console.log(file.get("coverFlag"));
                        // layer.msg('添加成功',{icon:1});
                        var index = parent.layer.getFrameIndex(window.name);
                        // parent.layer.close(index);
                        parent.location.reload(index);
                    }
                    else
                    {
                        layer.msg("修改算法失败："+responseText.message);
                    }
                },
                error:function(responseText) {
                    layer.msg("修改算法失败："+responseText.message);
                },
            })
        }, function(){
            var fileinfo=document.getElementById("file");
            file.append("coverFlag",true);
            file.append('file',fileinfo.files[0]);
            $.ajax({
                type:"POST",
                url:constUrl+"RegisterAlgorithm",
                processData: false,
                contentType: false,
                // dataType:"json",
                data: file,
                success:function(responseText){
                    var res = JSON.stringify(responseText);
                    responseText=eval("("+responseText+")");
                    if(responseText.result==true)
                    {
                        console.log(file.get("coverFlag"));
                        // layer.msg('添加成功',{icon:1});
                        var index = parent.layer.getFrameIndex(window.name);
                        // parent.layer.close(index);
                        parent.location.reload(index);
                    }
                    else
                    {
                        layer.msg("修改算法失败："+responseText.message);
                    }
                },
                error:function(responseText) {
                    layer.msg("修改算法失败："+responseText.message);
                },
            })
        });
    }

}

//验证准备
//输入验证器
function InputValidators(){
    this.validators = [];
    this.strategies = {};
}

//添加验证方法
//参数:
//  rule: 验证策略字符串
//  element: 被验证的dom元素
//  errMsg: 验证失败时显示的提示信息
//  value: 被验证的值
InputValidators.prototype.addValidator = function(rule, element, errMsg, value) {
    var that = this;
    var ruleElements = rule.split(":");

    this.validators.push(function() {
        var strategy = ruleElements.shift();
        var params = ruleElements;
        params.unshift(value);
        params.unshift(errMsg);
        params.unshift(element);

        return that.strategies[strategy].apply(that, params);
    });
};

//添加验证策略函数
//参数:
//  name: 策略名称
//  strategy: 策略函数
InputValidators.prototype.addValidationStrategy = function(name, strategy){
    this.strategies[name] = strategy;
};

//从策略对象导入验证策略函数
//参数:
//  strategies: 包含各种策略函数的对象
InputValidators.prototype.importStrategies = function(strategies) {
    for(var strategyName in strategies) {
        this.addValidationStrategy(strategyName, strategies[strategyName]);
    }
};

//验证失败时，将相关的错误信息打包返回
//参数:
//  element: dom元素
//   errMsg: 验证失败时的提示消息
//    value: 被验证的值
InputValidators.prototype.buildInvalidObj = function(element, errMsg, value){
    return {
        'value': value,
        'element': element,
        'errMsg': errMsg
    };
};

//开始验证
InputValidators.prototype.check = function() {
    for(var i = 0, validator; validator = this.validators[i++];){
        var result = validator();
        if(result) {
            return result;
        }
    }
};

//验证策略对象，包含默认的验证策略函数
var validationStrategies = {
    isNoEmpty: function(element, errMsg, value) {
        if(value === '') {
            return this.buildInvalidObj(element, errMsg, value );
        }
    },
    isNoUndefined: function(element, errMsg, value) {
        if(value === "undefined") {
            return this.buildInvalidObj(element, errMsg, value );
        }
    },
    isInteger:function (element, errMsg, value) {
        var reg = /^[0-9]*[1-9][0-9]*$/ ;
        if(!reg.test(value)){
            return this.buildInvalidObj(element, errMsg, value);
        }
    },
    isRight:function (element, errMsg, value) {
        if(value>100){
            return this.buildInvalidObj(element, errMsg, value);
        }
    },
    isNotEmpty:function (element, errMsg, value) {
        var fdstrat = value.indexOf(".");
        if(fdstrat==-1||value.length<=1){
            return this.buildInvalidObj(element, errMsg, value);
        }
    },
    isCheck:function (element, errMsg, value) {
        var isValue="";
        for(var i=0;i<value.length;i++)
        {
            if(value[i].checked)
            {
                isValue=value[i].value;
                break;
            }
            else
            {
                continue;
            }
        }

        if(isValue==""){
            return this.buildInvalidObj(element, errMsg, value);
        }
    },
    minLength: function(element, errMsg, value, length) {
        if(value.length < length){
            return this.buildInvalidObj(element, errMsg, value);
        }
    },

    maxLength: function(element, errMsg, value, length) {
        if(value.length > length){
            return this.buildInvalidObj(element, errMsg, value);
        }
    },

    isMail: function(element, errMsg, value, length) {
        var reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
        if(!reg.test(value)){
            return this.buildInvalidObj(element, errMsg, value);
        }
    }
};

function fileShowMess() {
    var paramFlieresString=$("#file").val();
    var str = paramFlieresString.split("\\");
    var paramFlieres = str[str.length-1];
    if(paramFlieres!=""){
        $('#fileShow_div').html(paramFlieres);
        $("#fileShow_div").show();
        $("#version").attr("disabled",false);
    }else{
        $('#fileShow_div').html('您还没有输入任何东西');
        $("#fileShow_div").show();
        $("#version").attr("disabled",true);
    }
}
function closeLayer() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

$("input[name=MPI]").click(function(){
    switch($("input[name=MPI]:checked").attr("id")){
        case "is_use_mpi":
            $("#cpunum_div").show();
            break;
        case "no_mpi":
            document.getElementById("cpunum").value = "";
            $("#cpunum_div").hide();

            break;
        default:
            break;
    }
});


